//
//  GridTreeNodeTableViewCell.swift
//  grid-ios-app
//
//  Created by wondertek on 2024/5/20.
//

import UIKit
import SnapKit

internal protocol GridTreeNodeTableViewCellDelegate: NSObjectProtocol {
    
    // row cell位置
    func gridTreeNodeTableViewCellMergeClick(_ indexPath: IndexPath)

}

class GridTreeNodeTableViewCell: UITableViewCell {

    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        setupLayouts()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    internal weak var delegate: GridTreeNodeTableViewCellDelegate?

    
    //保存约束的引用
    var leftConstraint: Constraint?
    
    var cellModel :GridTreeNodeModel? {
        didSet {
            titleLabel.text = cellModel?.companyName ?? ""
            if let model = cellModel {
                if model.isLeaf()  {
                    leftImageView.isHidden = true

                }else {
                    leftImageView.isHidden = false
                    if model.isExpand {
                        leftImageView.image = UIImage(named: "tree_ex")
                    }else {
                        leftImageView.image = UIImage(named: "tree_ec")
                    }
                }
                
                self.leftConstraint?.update(offset: model.level * 12)
            }
           
        }
    }
    
    var indexPath: IndexPath?
    
    
    func setupLayouts() {
        
        leftImageView.snp.makeConstraints { (make) in
            self.leftConstraint = make.left.equalToSuperview().offset(12).constraint
            make.centerY.equalToSuperview()
            make.width.equalTo(8)
            make.height.equalTo(14)
        }
        
        titleLabel.snp.makeConstraints { (make) in
            make.left.equalTo(leftImageView.snp.right).offset(10)
            make.height.equalTo(20)
            make.centerY.equalToSuperview()
            make.right.equalToSuperview().offset(-10)
        }

        mergeBtn.snp.makeConstraints { make in
            make.left.equalToSuperview().offset(8)
            make.right.equalTo(self.leftImageView.snp.right)
            make.top.bottom.equalToSuperview()
        }
        
        lineView.snp.makeConstraints { (make) in
            make.bottom.left.right.equalToSuperview()
            make.height.equalTo(1)
        }
    }
    
    private lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor(hex6: 0xF1F1F1)
        contentView.addSubview(view)
        return view
    }()
    
    private lazy var titleLabel: UILabel = {
        let label = UILabel()
        label.backgroundColor = .clear
        label.textColor = UIColor(hex6:0x404040)
        label.font = Theme.Font.size(16)
        label.text = ""
        contentView.addSubview(label)
        return label
    }()
    
    lazy var leftImageView: UIImageView = {
        let view = UIImageView(image: UIImage(named: "tree_ec"))
        contentView.addSubview(view)
        return view
    }()
    
    // 点击合并，展开
    lazy var mergeBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.backgroundColor = .clear
        button.addTarget(self, action: #selector(onClickButton(_:)), for: .touchUpInside)
        addSubview(button)
        return button
    }()
    
    @objc func onClickButton(_ sender: UIButton) {
        if sender == mergeBtn {
            if let model = cellModel , let indexNewPath = indexPath {
                // 不是子节点
                if !model.isLeaf()  {
                    delegate?.gridTreeNodeTableViewCellMergeClick(indexNewPath)
                }
            }
        }
    }
}
